package csc.fresher.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import csc.fresher.dao.SavingAccountTransactionDao;
import csc.fresher.entity.AccountHasInterestRate;
import csc.fresher.entity.AccountHasInterestRateState;
import csc.fresher.entity.AccountState;
import csc.fresher.entity.AccountTransactionState;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.SavingAccount;
import csc.fresher.entity.SavingAccountHasInterestRate;
import csc.fresher.entity.SavingInterestRate;
import csc.fresher.entity.SavingTransaction;
import csc.fresher.entity.SavingTransactionType;
import csc.fresher.entity.Transaction;
import csc.fresher.entity.TransactionState;
import csc.fresher.utils.DBUtils;

@Repository
public class SavingAccountTransactionDaoImpl implements
		SavingAccountTransactionDao {

	@Autowired
	DataSource dataSource;

	private static final Logger logger = Logger
			.getLogger(SavingAccountTransactionDaoImpl.class);

	
	@Override
	public long createTransaction(Transaction transaction,
			AccountHasInterestRate accountHasInterestRate) {
		if (!(transaction instanceof SavingTransaction)) {
			return -1;
		}

		SavingTransaction savingTransaction = (SavingTransaction) transaction;

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {

			connection = dataSource.getConnection();
			connection.setAutoCommit(false);
			
			long transactionID = 0;

			// update account transaction state
			if (!updateSavingAccountTransactionState(connection,
					AccountTransactionState.HOLD,
					savingTransaction.getCustomerAccountNumber())) {
				connection.rollback();
				return 0;
			}

			// insert transaction to db
			transactionID = insertTransaction(connection, savingTransaction);
			if (transactionID <= 0) {
				connection.rollback();
				return 0;
			}
			

			// insert account interest rate
			if (savingTransaction.getSavingTransactionType().getType().equals(SavingTransactionType.SAVING.getType())) {
				accountHasInterestRate.setState(AccountHasInterestRateState.PENDING);
				if (!insertSavingAccountHasInteresRate(connection, accountHasInterestRate)) {
					connection.rollback();
					return 0;
				}
			}
			
			connection.commit();
			return transactionID;
		} catch (SQLException e) {
			logger.error(e);
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			DBUtils.closeConnection(connection, preparedStatement, logger);
		}
		return 0;
	}

	@Override
	public void removeTransaction(long txID, long adminID, long accountID) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean approveTransaction(long transactionID, long adminID, long accountID) {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			connection.setAutoCommit(false);

			SavingTransaction savingTransaction = getSavingTransactionByID(connection, transactionID);
			
			// update transaction info
			if (!updateTransactionState(connection, transactionID, adminID, accountID, TransactionState.DONE)) {
				connection.rollback();
				return false;
			}
			 
			//SavingTransaction lastSavingTransaction = getLastSavingTransactionIsApproved(connection, accountID);
			//CustomerAccount customerAccount = getCustomerAccountByID(connection, accountID);
			
			double balance = 0;
			
			// update account transaction state
			if(savingTransaction.getSavingTransactionType().getType().equals(SavingTransactionType.WITHDRAW.getType())){
				balance = savingTransaction.getBeforeTxBalance() - savingTransaction.getFunds();
				if (!updateSavingAccountTransactionState(connection,
						AccountTransactionState.ACTIVE, accountID)) {
					connection.rollback();
					return false;
				}
				
				if (!updateSavingAccountBalance(connection, balance, accountID)) {
					connection.rollback();
					return false;
				}
			}

			if(savingTransaction.getSavingTransactionType().getType().equals(SavingTransactionType.SAVING.getType())){
				balance = savingTransaction.getBeforeTxBalance() + savingTransaction.getFunds();
				if(!updateSavingAccountHasInteresRateState(connection, AccountHasInterestRateState.ACCEPTED, accountID)){
					connection.rollback();
					return false;
				}
				
				if (!updateSavingAccountBalance(connection, balance, accountID)) {
					connection.rollback();
					return false;
				}
			}
			

			connection.commit();
		} catch (SQLException e) {
			logger.error(e);
			try {
				connection.rollback();
			} catch (SQLException e1) {
				logger.error(e);
			}
		} finally {
			DBUtils.closeConnection(connection, preparedStatement, logger);
		}
		return false;
	}

	@Override
	public boolean denyTransaction(long txID, long adminID, long accountID) {
		String query = "UPDATE Transaction SET `TransactionState` = ?,`SystemUser_SystemUserID1` = ?, `EndTime` = ? where `TransactionID` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			connection.setAutoCommit(false);
			// update transaction info
			preparedStatement.setString(1, TransactionState.DENY.toString());
			preparedStatement.setLong(2, adminID);
			preparedStatement.setTimestamp(3,
					new Timestamp(System.currentTimeMillis()));
			preparedStatement.setLong(4, txID);

			preparedStatement.execute();

			// update account state
			if (!updateSavingAccountTransactionState(connection,
					AccountTransactionState.ACTIVE, accountID)) {
				connection.rollback();
			}

			connection.commit();
		} catch (SQLException e) {
			logger.error(e);
			try {
				connection.rollback();
			} catch (SQLException e1) {
				logger.error(e);
			}
		} finally {
			DBUtils.closeConnection(connection, preparedStatement, logger);
		}
		return false;
	}

	@Override
	public List<Transaction> searchTransactionByAccountID(long accountID) {
		String query = "Select DISTINCT * from " + "SavingTransaction t "
				+ " WHERE t.`SavingAccountNumber` = ?";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		List<Transaction> listTransaction = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, accountID);
			ResultSet rs = preparedStatement.executeQuery();
			listTransaction = new ArrayList<Transaction>();
			Transaction tx = null;
			while (rs.next()) {
				tx = getTransactionFromResultSet(rs);
				if (tx != null) {
					listTransaction.add(tx);
				}
			}
			return listTransaction;
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}

				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
		return listTransaction;
	}

	@Override
	public List<Transaction> getTransactionByCustomerCardID(
			long customerCardID) {
		// TODO Auto-generated method stub
		return new ArrayList<Transaction>();
	}

	@Override
	public List<Transaction> getTransactionByCustomerCardIDInGivenPeriod(
			long customerCardID, Timestamp beginDate, Timestamp endDate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Transaction> getAllTransactionsByAccountIDInGivenPeriod(
			long accountID, Timestamp beginDate, Timestamp endDate) {
		String query = "Select DISTINCT * from "
				+ "SavingTransaction t "
				+ " WHERE t.`SavingAccountNumber` = ? and t.`EndTime` >= ? OR t.`BeginTiime` <= ?"
				+ " ORDER BY t.`BeginTiime` DESC";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, accountID);
			preparedStatement.setTimestamp(2, beginDate);
			preparedStatement.setTimestamp(3, endDate);
			ResultSet rs = preparedStatement.executeQuery();
			List<Transaction> listTransaction = new ArrayList<Transaction>();
			Transaction tx = null;
			while (rs.next()) {
				tx = getTransactionFromResultSet(rs);
				if (tx != null) {
					listTransaction.add(tx);
				}
			}
			return listTransaction;
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}

				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return new ArrayList<Transaction>();
	}

	private SavingTransaction getTransactionFromResultSet(ResultSet rs) {
		SavingTransaction tx = new SavingTransaction();
		try {
			tx.setTransactionID(rs.getInt("SavingTransactionID"));
			tx.setFunds(rs.getDouble("Funds"));
			tx.setCreatedDate(rs.getTimestamp("CreatedDate"));
			tx.setBeginTime(rs.getTimestamp("BeginTiime"));
			tx.setEndTime(rs.getTimestamp("EndTime"));
			tx.setSavingTransactionType(SavingTransactionType.getFromType(rs
					.getString("SavingTransactionType")));
			tx.setCustomerAccountNumber(rs.getLong("SavingAccountNumber"));
			tx.setTransactionState(TransactionState.valueOf(rs.getString(
					"TransactionState").toUpperCase()));
			tx.setBeforeTxBalance(rs.getDouble("BeforeTxBalance"));
			tx.setCreatedBy(rs.getInt("CreatedBy"));
			tx.setVerifiedBy(rs.getInt("ApprovedBy"));

		} catch (SQLException e) {
			logger.error(e);
		}
		return tx;
	}

	@Override
	public List<Transaction> getListTransactionsByAccountID(long accountID,
			int startIndex, int maxReturn) {
		String query = "Select DISTINCT * from "
				+ "SavingTransaction t "
				+ " WHERE t.`SavingAccountNumber` = ?  ORDER BY t.`BeginTiime` DESC LIMIT ?, ?";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		List<Transaction> listTransaction = new ArrayList<Transaction>();
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, accountID);
			preparedStatement.setInt(4, startIndex);
			preparedStatement.setInt(5, maxReturn);
			ResultSet rs = preparedStatement.executeQuery();
			Transaction tx = null;
			while (rs.next()) {
				tx = getTransactionFromResultSet(rs);
				if (tx != null) {
					listTransaction.add(tx);
				}
			}
			return listTransaction;
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}

				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
		return listTransaction;
	}

	@Override
	public List<Transaction> getListTransactionsByAccountIDInGivenPeriod(
			long accountID, Timestamp beginDate, Timestamp endDate,
			int startIndex, int maxReturn) {
		String query = "Select DISTINCT * from "
				+ "SavingTransaction t "
				+ " WHERE t.`SavingAccountNumber` = ? and t.`EndTime` >= ? OR t.`BeginTiime` <= ?"
				+ " ORDER BY t.`BeginTiime` DESC LIMIT ?, ?";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		List<Transaction> listTransaction = new ArrayList<Transaction>();
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, accountID);
			preparedStatement.setTimestamp(2, beginDate);
			preparedStatement.setTimestamp(3, endDate);
			preparedStatement.setInt(4, startIndex);
			preparedStatement.setInt(5, maxReturn);
			ResultSet rs = preparedStatement.executeQuery();

			Transaction tx = null;
			while (rs.next()) {
				tx = getTransactionFromResultSet(rs);
				if (tx != null) {
					listTransaction.add(tx);
				}
			}
			return listTransaction;
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}

				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return listTransaction;
	}
	
	@Override
	public SavingTransaction getLastSavingTransactionIsApproved(long accountID) {
		String query = "SELECT st.* FROM `savingtransaction` st LEFT JOIN `savingaccount` sa ON st.`SavingAccountNumber` = sa.`SavingAccountNumber` "
				+ " WHERE sa.`SavingAccountNumber`=? AND st.`TransactionState` = 'done' ORDER BY st.`CreatedDate` DESC LIMIT 1";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			connection = dataSource.getConnection();
			SavingTransaction savingTransaction = null;
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, accountID);
			rs = preparedStatement.executeQuery();
			if(rs.next()){
				savingTransaction = getTransactionFromResultSet(rs);
			}
			
			return savingTransaction;
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}

				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return null;
	}
	
	private boolean updateSavingAccountTransactionState(Connection connection,
			AccountTransactionState accountTransactionState,
			long savingAccountNumber) {
		String query = "UPDATE SavingAccount SET `AccountTransactionState` = ?, `EndTime` = ? where `SavingAccountNumber` = ?";

		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, accountTransactionState.getState());
			preparedStatement.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
			preparedStatement.setLong(3, savingAccountNumber);
			preparedStatement.execute();
			return true;
		} catch (SQLException e) {
			logger.error(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return false;
	}

	private boolean updateSavingAccountBalance(Connection connection,
			double balance, long savingAccountNumber) {
		String query = "UPDATE SavingAccount SET `SavingAccountBalance` = ? where `SavingAccountNumber` = ?";

		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setDouble(1, balance);
			preparedStatement.setLong(2, savingAccountNumber);
			preparedStatement.execute();
			return true;
		} catch (SQLException e) {
			logger.error(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return false;
	}
	
	private boolean updateSavingAccountHasInteresRateState(Connection connection,
			AccountHasInterestRateState accountHasInterestRateState, long savingAccountNumber) {
		String query = "UPDATE SavingAccount_has_InterestRate SET `State` = ? where `SavingAccount_AccNumber` = ? ORDER BY `FromDate` DESC LIMIT 1 ";

		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, accountHasInterestRateState.getState());
			preparedStatement.setLong(2, savingAccountNumber);
			preparedStatement.execute();
			return true;
		} catch (SQLException e) {
			logger.error(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return false;
	}

	private boolean insertSavingAccountHasInteresRate(Connection connection,
			AccountHasInterestRate accountHasInterestRate) {
		String query = "INSERT INTO `SavingAccount_has_InterestRate`(`SavingAccount_AccNumber`,`InterestRate_InterestRateID`,`FromDate`, `ToDate`, `Balance`, `State`) VALUES (?,?,?,?,?,?)";

		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, accountHasInterestRate.getAccountNumber());
			preparedStatement.setInt(2, accountHasInterestRate.getInterestRateID());
			preparedStatement.setTimestamp(3, accountHasInterestRate.getFromDate());
			preparedStatement.setTimestamp(4, accountHasInterestRate.getToDate());
			preparedStatement.setDouble(5, accountHasInterestRate.getBalance());
			preparedStatement.setString(6, accountHasInterestRate.getState().getState());
			preparedStatement.execute();
			return true;
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return false;
	}
	
	public SavingAccountHasInterestRate getInterestRateByAccountNumberID(Connection connection, long accountNumberID) {
		String query = "SELECT SavingInterestRateID, si.`createdDate`, CurrencyID, PeriodTypeID, Rate, si.`FromDate` AS siFromDate,  si.`ToDate` AS siToDate, shi.* "
				+ " FROM `savingaccount` sa LEFT JOIN `savingaccount_has_interestrate` shi"
				+ " ON sa.`SavingAccountNumber` = shi.`SavingAccount_AccNumber`"
				+ " LEFT JOIN `savinginterestrate` si"
				+ " ON si.`SavingInterestRateID` = shi.`InterestRate_InterestRateID`"
				+ " WHERE sa.`SavingAccountNumber`= ? AND shi.`State` == 'accepted' "
				+ " ORDER BY si.`FromDate` DESC ";
		SavingAccountHasInterestRate savingAccountHasInterestRate = null;
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, accountNumberID);
			rs = preparedStatement.executeQuery();
			if (rs.next()){
				SavingInterestRate savingInterestRate = new SavingInterestRate();
				savingInterestRate.setCreatedDate(rs.getDate("createdDate"));
				savingInterestRate.setCurrencyID(rs.getInt("CurrencyID"));
				savingInterestRate.setFromDate(rs.getDate("siFromDate"));
				savingInterestRate.setInterestRateID(rs.getInt("SavingInterestRateID"));
				savingInterestRate.setPeriodTypeID(rs.getInt("PeriodTypeID"));
				savingInterestRate.setRate(rs.getDouble("Rate"));
				savingInterestRate.setToDate(rs.getDate("siToDate"));
				
				savingAccountHasInterestRate = new SavingAccountHasInterestRate();
				savingAccountHasInterestRate.setAccountNumber(rs.getLong("SavingAccount_AccNumber"));
				savingAccountHasInterestRate.setBalance(rs.getDouble("Balance"));
				savingAccountHasInterestRate.setFromDate(rs.getTimestamp("FromDate"));
				savingAccountHasInterestRate.setToDate(rs.getTimestamp("siToDate"));
				savingAccountHasInterestRate.setInterestRateID(rs.getInt("SavingInterestRateID"));
				savingAccountHasInterestRate.setInterestRate(savingInterestRate);
			}
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return savingAccountHasInterestRate;
	}
	

	private long insertTransaction(Connection connection,
			Transaction transaction) {
		if (!(transaction instanceof SavingTransaction)) {
			return -1;
		}
		SavingTransaction savingTransaction = (SavingTransaction) transaction;

		String query = "INSERT INTO `SavingTransaction` (`Funds`, `CreatedDate`, `BeginTiime`,`EndTime`,`SavingTransactionType`,"
				+ "`SavingAccountNumber`, `TransactionState`,`BeforeTxBalance`,"
				+ "`CreatedBy`,`ApprovedBy`) VALUES (?,?,?,?,?,?,?,?,?,?)";

		PreparedStatement preparedStatement = null;
		try {
			// get current balance
			Timestamp currentTime = new Timestamp(System.currentTimeMillis());

			// insert transaction to db
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setDouble(1, savingTransaction.getFunds());
			preparedStatement.setTimestamp(2, currentTime);
			preparedStatement.setTimestamp(3, currentTime);
			preparedStatement.setTimestamp(4, currentTime);
			preparedStatement.setString(5, savingTransaction
					.getSavingTransactionType().getType());
			preparedStatement.setLong(6,
					savingTransaction.getCustomerAccountNumber());
			preparedStatement.setString(7, TransactionState.HOLD.getState());
			preparedStatement.setDouble(8,
					savingTransaction.getBeforeTxBalance());
			preparedStatement.setLong(9, savingTransaction.getCreatedBy());
			preparedStatement.setLong(10, savingTransaction.getVerifiedBy());

			preparedStatement.execute();
			ResultSet generateKey = preparedStatement.getGeneratedKeys();
			if (generateKey.next()) {
				return generateKey.getLong(1);
			}
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return 0;
	}

	private boolean updateTransactionState(Connection connection, long txID,
			long verifyByID, long accountID, TransactionState transactionState ) {
		String query = "UPDATE `SavingTransaction` SET `TransactionState` = ?,`ApprovedBy` = ?, `EndTime` = ? where `SavingTransactionID` = ?";
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, transactionState.getState());
			preparedStatement.setLong(2, verifyByID);
			preparedStatement.setTimestamp(3,
					new Timestamp(System.currentTimeMillis()));
			preparedStatement.setLong(4, txID);

			preparedStatement.execute();
			
			return true;
		} catch (SQLException e) {
			logger.error(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}

		return false;
	}
	
	public SavingTransaction getSavingTransactionByID(Connection connection, long transactionID) {
		String query = "SELECT * FROM `savingtransaction` WHERE `savingtransaction`.`SavingTransactionID`=?";
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			SavingTransaction savingTransaction = null;
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, transactionID);
			rs = preparedStatement.executeQuery();
			if(rs.next()){
				savingTransaction = getTransactionFromResultSet(rs);
			}
			
			return savingTransaction;
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		return null;
	}
	
	
	
	public CustomerAccount getCustomerAccountByID(Connection connection, long cutomerAccountID){
		String query = "SELECT * FROM `SavingAccount` WHERE `SavingAccount`.`SavingAccountNumber`=?";
		SavingAccount savingAccount = null;
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, cutomerAccountID);
			rs = preparedStatement.executeQuery();
			if (rs.next())
				savingAccount = getSavingAccountResultSet(rs);
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			DBUtils.closeResultSetConnection(connection, preparedStatement, rs,
					logger);
		}
		return savingAccount;
	}
	
	private SavingAccount getSavingAccountResultSet(ResultSet rs)
			throws SQLException {
		SavingAccount savingAccount = new SavingAccount();
		savingAccount.setAccountNumber(rs.getLong("SavingAccountNumber"));
		savingAccount.setAccountType(rs.getByte("AccountType"));
		savingAccount.setAccountBalance(rs.getDouble("SavingAccountBalance"));
		savingAccount.setRepeatable(rs.getBoolean("isRepeatable"));
		savingAccount.setCreatedDate(rs.getTimestamp("CreatedDate"));
		savingAccount.setAccountState(AccountState.valueOf(rs.getString(
				"AccountState").toUpperCase()));
		savingAccount
				.setAccountTransactionState(AccountTransactionState.valueOf(rs
						.getString("AccountTransactionState").toUpperCase()));
		savingAccount.setCustomerID(rs.getLong("Customer_CustomerID"));
		return savingAccount;
	}

}
